package swu.edu.cn.javaee.service;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.Tcomment;
import swu.edu.cn.javaee.entity.TcommentInfo;
import swu.edu.cn.javaee.mapper.TcommentMapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;
@Service
public class TcommentService {
    @Resource
    private TcommentMapper tcommentMapper;

    //新增评论
    public String saveTcomment(String comment,String uid,String tid,String parent_id){
        Tcomment tcomment=new Tcomment();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tcomment.setComment(comment);
        tcomment.setUid(uid);
        tcomment.setTid(tid);
        tcomment.setParent_id(parent_id);
        tcomment.setDate(format.format(date));
        tcommentMapper.saveTcomment(tcomment);
        System.out.println(tcomment);
        return SUCCESS_CODE;
    }

    //查看topic下所有评论
    public TcommentInfo queryTcommentList(String tid){
        List<Tcomment> tcommentList= tcommentMapper.queryTcommentList(tid);
        Integer counts=tcommentMapper.counts(tid);
        TcommentInfo tcommentInfo=new TcommentInfo();
        if (tcommentList.size()!=0){
            tcommentInfo.setCode("0");
            tcommentInfo.setMsg("");
            tcommentInfo.setCount(String.valueOf(counts));
            tcommentInfo.setData(tcommentList);
            System.out.println(tcommentInfo);
            return tcommentInfo;
        }
        else {
            return null;
        }
    }


    //删除评论
    public String deleteTcomment(String tm_id){
        Tcomment tcomment=tcommentMapper.queryTcommentById(tm_id);
        if (tcomment!=null){
            tcommentMapper.deleteTcomment(tcomment.getTm_id());
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_NO_COMMENT;
        }
    }
}
